package dto

import (
	"code.bydev.io/cht/customer/kyc_admin_service/internal/pkg/kycprovider/dto"
	"git.bybit.com/svc/stub/pkg/pb/api/consts/euser"
	kyc2 "git.bybit.com/svc/stub/pkg/pb/api/kyc"
	"git.bybit.com/svc/stub/pkg/pb/enums/ekycadmin"
	"git.bybit.com/svc/stub/pkg/pb/enums/kyc"
)

type KYCEventType int32

const (
	Unspecific KYCEventType = iota
	Init
	Create
	Pending
	Complete
	Reset
	Amend
	IdentifyTransfer
	Level2StateComplete
)

type ResultDetail struct {
	FinalDecision int    `json:"final_decision"`
	FinalReason   string `json:"final_reason"`
	ReviewTime    string `json:"review_time"`
	ReviewTimeMs  int64  `json:"review_time_ms,omitempty"`
}

type ClientDetail struct {
	IP     string `json:"ip"`
	Device string `json:"device"`
}

type EventSource int32

const (
	EventSourceUnknown EventSource = iota
	EventSourceProvider
	EventSourceUser
	EventSourceCSPortal
	EventSourceMigrate
)

type RawResponse struct {
	One    string `json:"one"`
	Status string `json:"status"`
}

type ExtraData struct {
	RawResponse string `json:"raw_response"`
}

type KYCEventNotify struct {
	MemberID      int64                    `json:"member_id"`
	BrokerID      euser.BrokerID           `json:"broker_id,omitempty"`
	ExternalUID   string                   `json:"external_uid"`
	Provider      kyc.KYCProvider          `json:"provider"`
	AccountID     string                   `json:"account_id"`
	RecordID      string                   `json:"record_id"`
	Level         int                      `json:"level"`
	EventType     ekycadmin.KYCAdminAction `json:"event_type"`
	ResultDetail  ResultDetail             `json:"result_detail"`
	ClientDetail  ClientDetail             `json:"client_detail"`
	EventTime     string                   `json:"event_time"`
	EventTimeMs   int64                    `json:"event_time_ms,omitempty"`
	From          EventSource              `json:"from"`
	ExtraData     ExtraData                `json:"extra_data"`
	RawResponse   RawResponse              `json:"raw_response"`
	IsAmend       bool                     `json:"is_amend"`
	LevelName     string                   `json:"level_name"`
	PoaIDDocType  string                   `json:"poa_id_doc_type"` // PoI as PoA 证件类型(只有Lv2有)
	SelectDocType int8                     `json:"select_doc_type"` // 选择的证件类型
	SelectCountry string                   `json:"select_country"`  // 选择的国家
	BusinessID    string                   `json:"business_id"`     // 业务ID
}

func (e *KYCEventNotify) IsFromMigrate() bool {
	return e.From == EventSourceMigrate
}

func (e *KYCEventNotify) GetSerialType() dto.SerialType {
	if e.IsSecondary() {
		return dto.SerialTypeSecondary
	}
	return dto.SerialTypePrimary
}

// IsSecondary Is secondary
func (e *KYCEventNotify) IsSecondary() bool {
	return dto.LvlNameIsSecondary(e.LevelName)
}

// IsSupplement Is supplement
func (e *KYCEventNotify) IsSupplement() bool {
	return dto.LvlNameIsSupplement(e.LevelName)
}

type KYCCommonState int8

const (
	KYCCommonStateDefault KYCCommonState = iota
	KYCCommonStatePending
	KYCCommonStateComplete
	KYCCommonStateReset
)

type KYCPubProvider int

const (
	// ProviderSumSub sumsub
	ProviderSumSub  KYCPubProvider = iota
	ProviderJumio   KYCPubProvider = 1
	ProviderOnfido  KYCPubProvider = 2
	ProviderUnknown KYCPubProvider = 9
)

// ConvertProvider Change KYCProvider to KYCPubProvider
func ConvertProvider(provider int8) kyc.KYCProvider {
	p := ProviderSumSub
	switch kyc.KYCProvider(provider) {
	case kyc.KYCProvider_PROVIDER_SUMSUB:
		p = ProviderSumSub
	case kyc.KYCProvider_PROVIDER_JUMIO:
		p = ProviderJumio
	case kyc.KYCProvider_PROVIDER_ONFIDO:
		p = ProviderOnfido
	}

	return kyc.KYCProvider(p)
}

type Transfer struct {
	TransID     string `json:"trans_id"`
	SrcMemberID int64  `json:"src_member_id"`
	SrcLeveL    int64  `json:"src_level"`
	TarMemberID int64  `json:"tar_member_id"`
	TarLevel    int64  `json:"tar_level"`
}

type KYCResultNotify struct {
	MemberID int64            `json:"member_id"`
	LeveL    int64            `json:"level"`
	Result   dto.ReviewResult `json:"result"`
	Transfer Transfer         `json:"transfer,omitempty"`
}

type FilecoreUploadNotify struct {
	MemberID     int64            `json:"member_id"`
	IdempotentID string           `json:"idempotent_id"`
	FileBizType  kyc2.FileBizType `json:"file_biz_type"`
}
